//
//  StorageShoppingCell.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/11/27.
//  Copyright © 2018 小河. All rights reserved.
//

#import "StorageShoppingCell.h"

@implementation StorageShoppingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self createView];
    }
    
    return self;
}

- (void)createView
{
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 1)];
    [self.contentView addSubview:lineView];
    lineView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    
    self.mainIconImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.mainIconImage];
    self.mainIconImage.backgroundColor = [UIColor lightGrayColor];
    self.mainIconImage.contentMode = UIViewContentModeScaleAspectFill;
    self.mainIconImage.clipsToBounds = YES;
    
    self.ziCaiLabel = [[UILabel alloc] init];
    [self.mainIconImage addSubview:self.ziCaiLabel];
    self.ziCaiLabel.backgroundColor = RGBACOLOR(239, 208, 83, 0.9);
    self.ziCaiLabel.font = FontWithSize(12);
    self.ziCaiLabel.textColor = [UIColor whiteColor];
    self.ziCaiLabel.textAlignment = 1;
    self.ziCaiLabel.text = @"自采";
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.textAlignment = 0;
    self.titleLabel.textColor = BlackTextColor;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = FontWithSize(14);
    
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.editBtn];
    [self.editBtn setBackgroundImage:[UIImage imageNamed:@"edit_goods"] forState:UIControlStateNormal];
    
    self.codeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.codeLabel];
    self.codeLabel.textAlignment = 0;
    self.codeLabel.textColor = RGBACOLOR(194, 194, 194, 1);
    self.codeLabel.font = FontWithSize(12);
    
    self.auditState = [[UILabel alloc] init];
    [self.contentView addSubview:self.auditState];
    self.auditState.textColor = RGBACOLOR(237, 73, 59, 1);
    self.auditState.font = FontWithSize(12);
    self.auditState.textAlignment = 2;
    self.auditState.hidden = YES;
    
    self.priceLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.priceLabel];
    self.priceLabel.textAlignment = 0;
    self.priceLabel.textColor = BlackTextColor;
    self.priceLabel.font = FontWithSize(14);
    
    self.auditMessageLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.auditMessageLabel];
    self.auditMessageLabel.textColor = RGBACOLOR(237, 73, 59, 1);
    self.auditMessageLabel.font = FontWithSize(12);
    self.auditMessageLabel.textAlignment = 0;
    
    self.putawayBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.putawayBtn];
    self.putawayBtn.backgroundColor = BtnBgColor;
    self.putawayBtn.layer.cornerRadius = 15;
    self.putawayBtn.layer.masksToBounds = YES;
    [self.putawayBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    self.putawayBtn.titleLabel.font = FontWithSize(12);
    
    self.delectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.delectBtn];
    self.delectBtn.backgroundColor = [UIColor whiteColor];
    self.delectBtn.layer.borderWidth = 1;
    self.delectBtn.layer.borderColor = BtnBgColor.CGColor;
    self.delectBtn.layer.cornerRadius = 15;
    self.delectBtn.layer.masksToBounds = YES;
    
    [self.delectBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    self.delectBtn.titleLabel.font = FontWithSize(12);
    
}

- (void)layoutSubviews
{
    self.mainIconImage.frame = CGRectMake(15, 15, 100, 100);
    self.ziCaiLabel.frame = CGRectMake(0, self.mainIconImage.height-30, self.mainIconImage.width, 30);
    self.editBtn.frame = CGRectMake(SCREENWIDTH-20-15, 20, 20, 20);
    self.titleLabel.frame = CGRectMake(self.mainIconImage.right+10, 15, SCREENWIDTH-20-15-10-100-10-15, 44);
    
    self.codeLabel.frame = CGRectMake(self.mainIconImage.right+10, self.titleLabel.bottom+5, 100, 12);
    [self.codeLabel sizeToFit];
    
    self.auditState.frame =CGRectMake(SCREENWIDTH-200-10, self.titleLabel.bottom+5, 200, 12);
    
    self.priceLabel.frame = CGRectMake(self.mainIconImage.right+10, self.codeLabel.bottom+24, 120, 14);
    [self.priceLabel sizeToFit];
    
    self.auditMessageLabel.frame = CGRectMake(15, self.priceLabel.bottom+3, SCREENWIDTH-30, 12);
    
    self.putawayBtn.frame = CGRectMake(SCREENWIDTH-70-15, self.titleLabel.bottom+26, 70, 30);
    self.delectBtn.frame = CGRectMake(self.putawayBtn.left-10-70, self.titleLabel.bottom+26, 70, 30);
}

- (void)setCellWithMode:(MainModel *)model
{
    [self.mainIconImage sd_setImageWithURL:[NSURL URLWithString:model.showImage] placeholderImage:[UIImage imageNamed:@"zhanwei100"]];
    
    self.titleLabel.text = [NSString stringWithFormat:@"%@",model.title];
    
    self.codeLabel.text = [NSString stringWithFormat:@"条码 %@",model.barcode];
    
    CGFloat price = [(NSString *)model.price floatValue];
    self.priceLabel.text = [NSString stringWithFormat:@"￥%0.2f",price];
    
    if ([model.type isEqualToString:@"1"]) {
        self.ziCaiLabel.hidden = NO;
    }
    else if ([model.type isEqualToString:@"2"]) {
        self.ziCaiLabel.hidden = YES;
    }
    
    if (self.index == 1)//1已上架商品 2审核商品 3仓库商品
    {//1已上架商品
        self.auditState.hidden = YES;
        self.auditMessageLabel.hidden = YES;
        [self.delectBtn setTitle:@"下架" forState:UIControlStateNormal];
        [self.putawayBtn setTitle:@"分享" forState:UIControlStateNormal];
    }
    else if (self.index == 3)
    {//3仓库商品
        self.auditState.hidden = YES;
        self.auditMessageLabel.hidden = YES;
        [self.delectBtn setTitle:@"删除" forState:UIControlStateNormal];
        [self.putawayBtn setTitle:@"上架" forState:UIControlStateNormal];
    }
    else if (self.index == 2)
    {//2审核商品
        self.auditState.hidden = NO;
        if ([model.state isEqualToString:@"2"])
        {//待审核
            self.delectBtn.hidden = YES;
            self.putawayBtn.hidden = YES;
            self.editBtn.hidden = YES;
            self.auditState.text = @"审核中";
            self.auditMessageLabel.hidden = YES;
            self.auditMessageLabel.text = @"审核失败";
        }
        else if ([model.state isEqualToString:@"3"])
        {//审核未通过
            self.delectBtn.hidden = NO;
            self.putawayBtn.hidden = NO;
            self.editBtn.hidden = NO;
            [self.delectBtn setTitle:@"删除" forState:UIControlStateNormal];
            [self.putawayBtn setTitle:@"上架" forState:UIControlStateNormal];
            self.auditState.text = @"审核失败";
            self.auditMessageLabel.hidden = NO;
            NSString *reviewErrorMessage = model.attributesMap[@"reviewErrorMessage"];
            if (NOEmptyStr(reviewErrorMessage))
            {
                self.auditMessageLabel.text = reviewErrorMessage;
            }
        }
        
    }
    
}

@end
